﻿package com.cn.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.cn.bean.Order;
import com.cn.service.OrderService;
import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionContext;

public class OrderAction {
	private Order order;
	private OrderService orderService = null;
	HttpServletResponse response;
	
	public Order getOrder() {
		return order;
	}
	public void setOrder(Order order) {
		this.order = order;
	}
	public OrderService getOrderService() {
		return orderService;
	}
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}
	/**
	 * 下单 
	 */
	public void placeOrder(){
		try {
			response = ServletActionContext.getResponse();
			response.setContentType("text/html;charset=utf-8");
			response.setCharacterEncoding("utf-8");
			String result = orderService.placeOrder();
			PrintWriter out = response.getWriter();
			out.print(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 秒杀
	 */
	public void placeSecondKillOrder(){
		try {
			response = ServletActionContext.getResponse();
			response.setContentType("text/html;charset=utf-8");
			response.setCharacterEncoding("utf-8");
			String result = orderService.placeSecondKillOrder();
			PrintWriter out = response.getWriter();
			out.print(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 返回个人的全部订单到安卓端
	 */
	public void getAllOrder(){
		try {
		List list = orderService.getAllOrders();
		Gson gson = new Gson();
		String json = gson.toJson(list);
		response = ServletActionContext.getResponse();
		response.setContentType("text/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.write(json);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 返回个人的未接单订单到安卓端
	 */
	public void getWaitTakeOrder(){
		try {
		List list = orderService.getWaitTakeOrder();
		Gson gson = new Gson();
		String json = gson.toJson(list);
		response = ServletActionContext.getResponse();
		response.setContentType("text/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.write(json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 返回个人的未确认订单到安卓端
	 */
	public void getWaitConfirmOrder(){
		try {
		List list = orderService.getWaitConfirmOrder();
		Gson gson = new Gson();
		String json = gson.toJson(list);
		response = ServletActionContext.getResponse();
		response.setContentType("text/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.write(json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 返回个人的未评价订单到安卓端
	 */
	public void getWaitRemarkOrder(){
		try {
		List list = orderService.getWaitRemarkOrder();
		Gson gson = new Gson();
		String json = gson.toJson(list);
		response = ServletActionContext.getResponse();
		response.setContentType("text/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.write(json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 返回个人的已完成订单到安卓端
	 */
	public void getCompletedOrder(){
		try {
		List list = orderService.getCompletedOrder();
		Gson gson = new Gson();
		String json = gson.toJson(list);
		response = ServletActionContext.getResponse();
		response.setContentType("text/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.write(json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 返回订单详情到安卓端
	 */
	public void orderDetail(){
		try {
		List list = orderService.orderDetail();
		Gson gson = new Gson();
		String json = gson.toJson(list);
		response = ServletActionContext.getResponse();
		response.setContentType("text/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.write(json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 评价订单
	 */
	public void remarkOrder(){
		try {
			response = ServletActionContext.getResponse();
			response.setContentType("text/html;charset=utf-8");
			response.setCharacterEncoding("utf-8");
			String result = orderService.remarkOrder();
			PrintWriter out = response.getWriter();
			out.print(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//供应商接受订单
	public String acceptOrder()
	{
		ActionContext ctx= ActionContext.getContext();
		int order_id=order.getId();
		if(orderService.acceptOrder(order_id))
			return "success";
		else
			return "fail";
	}
	//供应商拒绝订单
	public String rejectOrder()
	{
		ActionContext ctx= ActionContext.getContext();
		int order_id=order.getId();
		if(orderService.rejectOrder(order_id))
			return "success";
		else
			return "fail";
	}

	/**
	 * 确认订单
	 */
	public void confirmOrder(){
		try {
			response = ServletActionContext.getResponse();
			response.setContentType("text/html;charset=utf-8");
			response.setCharacterEncoding("utf-8");
			String result = orderService.confirmOrder();
			PrintWriter out = response.getWriter();
			out.print(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 24小时不接单自动取消订单
	 */
	public void autoCancel(){
		System.out.println("取消ing");
		orderService.autoCancel();
	}
}
